package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursing_project")
public class NursingProjectController extends BaseController{
    @Autowired
    private NursingProjectService nursingProjectService;

    @GetMapping
    public ResponseResult<NursingProjectVo> findByPage(
            @RequestParam(name = "name",required = false)String name,
            @RequestParam(name = "status",required = false)Integer status,
            @RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize
            ){
        PageResponse<NursingProjectVo> responseResult = nursingProjectService.findByPage(name,status,pageNum,pageSize);
        return success(responseResult);
    }

    @PostMapping
    public ResponseResult save(@RequestBody NursingProjectDto nursingProjectDto){
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectService.save(nursingProject);
        return success();
    }

    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable(name = "id")Long id){
        NursingProjectVo nursingProjectVo = nursingProjectService.findById(id);
        return success(nursingProjectVo);
    }

    @PutMapping
    public ResponseResult updateById(@RequestBody NursingProjectDto nursingProjectDto){
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectService.update(nursingProject);
        return success();
    }

    @PutMapping("/{id}/status/{status}")
    public ResponseResult updateStatus(
            @PathVariable(name = "id")Long id,
            @PathVariable(name = "status")Integer status
            ){
        nursingProjectService.updateStatus(id,status);
        return success();
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable(name = "id")Long id){
        nursingProjectService.deleteById(id);
        return success();
    }

    @GetMapping("/all")
    public ResponseResult<NursingProjectVo> findAll(){
        List<NursingProjectVo> list = nursingProjectService.findAll();
        return success(list);
    }
}
